package co.com.impresion.archivos;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.font.LineBreakMeasurer;
import java.awt.font.TextLayout;
import java.awt.geom.Point2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.text.AttributedCharacterIterator;
import java.text.AttributedString;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.ServiceUI;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.MediaSizeName;

public class Impresora implements Printable {

	private AttributedString texto;

	public AttributedString getTexto() {
		return texto;
	}

	public void setTexto(AttributedString texto) {
		this.texto = texto;
	}

	public void impresionArchivo(String rutaGenerada, AttributedString texto) {

		DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
		PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
		aset.add(MediaSizeName.ISO_A4);
		this.texto = texto;
		// PrintService pservices =
		// PrintServiceLookup.lookupDefaultPrintService();
		PrintService[] pservices = PrintServiceLookup.lookupPrintServices(null,
				null);
		PrintService defaultPrint = PrintServiceLookup
				.lookupDefaultPrintService();
		PrintService services = ServiceUI.printDialog(null, 200, 200,
				pservices, defaultPrint, flavor, aset);
		if (services != null) {
			DocPrintJob pj = services.createPrintJob();
			FileInputStream fis;
			try {
				fis = new FileInputStream(rutaGenerada);
				Doc doc = new SimpleDoc(fis, flavor, null);
				pj.print(doc, aset);
				File eliminarArchivo = new File(rutaGenerada);
				eliminarArchivo.delete();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (PrintException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public int print(Graphics g, PageFormat format, int arg2)
			throws PrinterException {
		// TODO Auto-generated method stub
		Graphics2D g2d = (Graphics2D) g;
		g2d.translate(format.getImageableX(), format.getImageableY());
		g2d.setPaint(Color.black);
		// g2d.setFont("font");
		Point2D.Float pen = new Point2D.Float();
		AttributedCharacterIterator charIterator = texto.getIterator();
		LineBreakMeasurer measurer = new LineBreakMeasurer(charIterator, g2d
				.getFontRenderContext());
		float wrapp = (float) format.getImageableWidth();
		while (measurer.getPosition() < charIterator.getEndIndex()) {
			TextLayout layout = measurer.nextLayout(wrapp);
			pen.y += layout.getAscent();
			float dx = layout.isLeftToRight() ? 0 : (wrapp - layout
					.getAdvance());
			layout.draw(g2d, pen.x + dx, pen.y);
			pen.y += layout.getDescent() + layout.getLeading();
		}
		return Printable.PAGE_EXISTS;
	}

}
